﻿#include "settinghandler.h"
#include <QDebug>
#include <QColor>
#include <QDir>
#include <QMessageBox>

#include "widget.h"

static SettingHandler* setting_handler = nullptr;
SettingHandler* SettingHandler::get_instance()
{
    if (nullptr == setting_handler)
        setting_handler = new SettingHandler();
    return setting_handler;
}

SettingHandler::SettingHandler()
    : lnk_size(54), widget_height(70), margin_bottom(100), widget_radius(15), show_clock(true), back_time(3),
      background_color(160, 160, 160, 200), border_color(150, 150, 150, 220), file(Widget::get_setting_filepath())
{
    qDebug() << "SettingHandler 创建了";
    load_setting_file();
}

QMap<QString, QStringList> SettingHandler::read_all()
{
    file.setFileName(Widget::get_setting_filepath());
    QMap<QString, QStringList> ret;
    if (! file.exists())
    {
        file.open(QIODevice::WriteOnly);
        file.close();
    }

    auto ok = file.open(QIODevice::ReadOnly);
    QString word = "";
    if (ok)
        word = file.readAll();

    file.close();
    auto setting_list = word.split('\n');
    for (const auto &str: setting_list)
    {
        auto one = str.split(',');

        if (one.size() > 1)
            ret.insert(one.at(0), QStringList(++one.begin(), one.end()));
    }
    return ret;
}

void SettingHandler::write_all()
{
    file.setFileName(Widget::get_setting_filepath());
    file.open(QIODevice::WriteOnly);
    file.write(("lnk_size," + QString::number(lnk_size) + "\n").toUtf8());
    file.write(("widget_height," + QString::number(widget_height) + "\n").toUtf8());
    file.write(("margin_bottom," + QString::number(margin_bottom) + "\n").toUtf8());
    file.write(("widget_radius," + QString::number(widget_radius) + "\n").toUtf8());
    file.write(("show_clock," + QString::number(show_clock) + "\n").toUtf8());
    file.write(("back_time," + QString::number(back_time) + "\n").toUtf8());

    file.write(("background_color," + QString::number(background_color.red()) + ","
                + QString::number(background_color.green()) + ","
                + QString::number(background_color.blue()) + ","
                + QString::number(background_color.alpha()) + "\n").toUtf8());

    file.write(("border_color," + QString::number(border_color.red()) + ","
                + QString::number(border_color.green()) + ","
                + QString::number(border_color.blue()) + ","
                + QString::number(border_color.alpha()) + "\n").toUtf8());

    file.close();
}

bool SettingHandler::load_setting_file()
{
    auto setting_map = read_all();

    auto v1 = setting_map.value("lnk_size", {});
    if (! v1.empty())
        lnk_size = v1.begin()->toInt();

    auto v2 = setting_map.value("widget_height", {});
    if (! v2.empty())
        widget_height = v2.begin()->toInt();

    auto v3 = setting_map.value("margin_bottom", {});
    if (! v3.empty())
        margin_bottom = v3.begin()->toInt();

    auto v6 = setting_map.value("widget_radius", {});
    if (! v6.empty())
        widget_radius = v6.begin()->toInt();

    auto v7 = setting_map.value("show_clock", {});
    if (! v7.empty())
        show_clock = v7.begin()->toInt();

    auto v8 = setting_map.value("back_time", {});
    if (! v8.empty())
        back_time = v8.begin()->toInt();

    auto v4 = setting_map.value("background_color", {});
    if (! v4.empty())
        background_color = QColor(v4.at(0).toInt(), v4.at(1).toInt(), v4.at(2).toInt(), v4.at(3).toInt());

    auto v5 = setting_map.value("border_color", {});
    if (! v5.empty())
        border_color = QColor(v5.at(0).toInt(), v5.at(1).toInt(), v5.at(2).toInt(), v5.at(3).toInt());

    return true;
}

SettingHandler::~SettingHandler()
{
    qDebug() << "SettingHandler 销毁了";
}
